﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using Commander.Classes;

using System.IO;
using Commander.Structures;

namespace Commander.Views
{
    //Надо добавить копирование по типу файла

    /// <summary>
    /// Логика взаимодействия для Moves.xaml
    /// </summary>
    public partial class Moves : Window
    {
        private Thread mov;
        private int Count;
        private int val;
        public delegate void movClose();

        private Info data;

        public Moves()
        {
            InitializeComponent();
        }

        public Moves(Info Data)
        {
            InitializeComponent();
            data = Data;
            Count = Work.CountFile(data);
            progressBar2.Minimum = 0;
            progressBar2.Maximum = Count;
            progressBar2.Value = val = 0;
        }

        private void move()
        {
            foreach(IOView item in data.files)
            {
                if (item.Size == "<Папка>")
                {
                    if (item.Name != "..")
                    {
                        MoveDir(data.OldPatch + item.Name, data.NewPatch + item.Name);
                    }
                }
                else
                {
                    MoveFile(data.OldPatch + item.Name + "." + item.Type, data.NewPatch + item.Name + "." + item.Type);
                }
            }
            this.Dispatcher.BeginInvoke(new movClose(clos), System.Windows.Threading.DispatcherPriority.Background);
        }

        private bool MoveFile(string oldName,string newName)
        {
            if (!Directory.Exists(System.IO.Path.GetDirectoryName(newName)))
            {
                try
                {
                    this.Dispatcher.BeginInvoke((Action)(() => 
                    { 
                        this.label2.Content = oldName;
                        this.label4.Content = newName; 
                    }));
                    Directory.CreateDirectory(System.IO.Path.GetDirectoryName(newName));
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                    return false;
                }
            }
            try
            {
                this.Dispatcher.BeginInvoke((Action)(() => 
                { 
                    this.label2.Content = oldName; 
                    this.label4.Content = newName;
                    progressBar2.Value = ++val;
                    this.label6.Content = (int)val * 100 / Count + "%";
                }));
                File.Copy(oldName, newName);
                File.Delete(oldName);
            }
            catch(Exception a)
            {
                MessageBox.Show(a.ToString());
                return false;
            }
            return true;
        }

        private bool MoveDir(string oldName,string newName)
        {
            Directory.CreateDirectory(newName);

            if (data.Type != "")
            {
                foreach (string s in Directory.GetFiles(oldName,data.Type))
                {
                    MoveFile(s, newName + @"\" + System.IO.Path.GetFileName(s));
                }
            }
            else
            {
                foreach (string s in Directory.GetFiles(oldName))
                {
                    MoveFile(s, newName + @"\" + System.IO.Path.GetFileName(s));
                }
            }

            foreach (string s in Directory.GetDirectories(oldName))
            {
                MoveDir(s, newName + @"\" + System.IO.Path.GetFileName(s));
            }

            DeleteDir(oldName);
            return true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mov = new Thread(new ThreadStart(move));
            mov.Start();
        }

        public void clos()
        {
            this.Close();
        }

        private bool DeleteDir(string FolderName)
        {
            if (!Directory.Exists(FolderName))
            {
                return true;
            }

            foreach (string s in Directory.GetDirectories(FolderName))
            {
                try
                {
                    DeleteDir(s);
                }
                catch(Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
            }
            try
            {
                Directory.Delete(FolderName);
            }
            catch(Exception a)
            {
                MessageBox.Show(a.ToString());
            }
            return !Directory.Exists(FolderName);
        }
    }
}
